package com.nodecollege.cloud.service.impl.deal;

import com.alibaba.fastjson.JSON;
import com.nodecollege.cloud.common.model.NCResult;
import com.nodecollege.cloud.common.model.po.ChatRecord;
import com.nodecollege.cloud.common.model.vo.ChatFriendData;
import com.nodecollege.cloud.common.utils.ThreadUserUtils;
import com.nodecollege.cloud.service.ChatService;
import com.nodecollege.cloud.service.RecordService;
import com.nodecollege.cloud.service.impl.ChatServiceAbstrat;
import io.netty.channel.Channel;
import io.netty.channel.group.ChannelGroup;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 好友聊天消息
 *
 * @author LC
 * @date 2020/2/26 23:07
 */
@Slf4j
@Service
public class ChatFriendServiceImpl extends ChatServiceAbstrat implements ChatService {

    @Autowired
    private RecordService recordService;

    /**
     * 消息处理
     *
     * @param chatData
     * @param channel
     */
    @Override
    public void handler(String chatData, Channel channel) {
        log.info("ChatFriendServiceImpl handler");
        ChatFriendData friendData = JSON.parseObject(chatData, ChatFriendData.class);
        ChatRecord record = new ChatRecord();
        BeanUtils.copyProperties(friendData, record);
        record.setSendUserId(ThreadUserUtils.getLoginUser().getUserId());
        log.info("消息 {}", record);
        record = recordService.addRecord(record);
        // 给自己发送
        sendMsg(record.getSendUserId(), NCResult.ok(record), 2);
        // 给朋友发送
        sendMsg(record.getReceiveUserId(), NCResult.ok(record), 2);
    }
}
